package com.sskj.b70.p.activity;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.http.JsonCallBack;
import com.sskj.b70.m.http.JsonConverter;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.activity.BindMobileActivity;


public class BindMobileActivityPresenter extends BasePresenter<BindMobileActivity> {

    public void sendCode(String mobile) {
        httpService.sendCode(mobile, null)
                .converter(new JsonConverter<HttpData>() {
                })
                .adapt(new FlowableBody<>())
                .subscribe(httpData -> {
                    ToastUtil.showShort(httpData.msg);
                    if (httpData.status == HttpConfig.STATUS_OK) {  // 0代表成功
                        mView.sendCodeOk();
                    } else {
                        mView.sendCodeError();
                    }
                }, e -> {
                    ToastUtil.showShort(e.getMessage());
                    mView.sendCodeError();
                });
    }

    public void bindMobile(String mobile, String code) {
        httpService.bindMobile(mobile,code)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == HttpConfig.STATUS_OK) {
                            ToastUtil.showShort(httpListData.getMsg());
                            mView.onResponseSuccess();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }
}
